/*
 * Copyright (C) 2015 grandcentrix GmbH
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package net.grandcentrix.tray.sample;

import net.grandcentrix.tray.AppPreferences;
import net.grandcentrix.tray.sample.slice.MainAbilitySlice;
import net.grandcentrix.tray.util.LogUtil;

import ohos.aafwk.ability.IntentAbility;
import ohos.aafwk.content.Intent;
import ohos.data.DatabaseHelper;
import ohos.data.preferences.Preferences;

public class MultiProcessService extends IntentAbility {

    private static final String TAG = MultiProcessService.class.getSimpleName();

    public static final String KEY_MULTIPROCESS_COUNTER_SERVICE_READ = "multiprocess_counter_read";

    public static final String KEY_MULTIPROCESS_COUNTER_SERVICE_WRITE = "multiprocess_counter_write";

    private static final String INTENT_MODE = "mode";

    public static final String INTENT_MODE_READ = "read";

    public static final String INTENT_MODE_WRITE = "write";

    private static int mCount = 0;

    private Preferences mSharedPreferences;

    private AppPreferences mTrayPreferences;

    public MultiProcessService() {
        super(TAG);
    }

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);

        mTrayPreferences = new AppPreferences(this);
        DatabaseHelper databaseHelper = new DatabaseHelper(this);
        mSharedPreferences = databaseHelper.getPreferences(MainAbilitySlice.SHARED_PREF_NAME);
    }

    @Override
    protected void onCommand(Intent intent, boolean restart, int startId) {
        super.onCommand(intent, restart, startId);

        LogUtil.info(TAG, "Entering onCommand "+restart);
    }

    @Override
    protected void onProcessIntent(final Intent intent) {
        final String mode = intent.getStringParam(INTENT_MODE);
        switch (mode) {
            case INTENT_MODE_READ: {
                final int trayCount = mTrayPreferences
                        .getInt(KEY_MULTIPROCESS_COUNTER_SERVICE_READ, 0);
                final int sharedPrefsCount = mSharedPreferences
                        .getInt(KEY_MULTIPROCESS_COUNTER_SERVICE_READ, 0);
                LogUtil.debug(TAG, "Entering read in other process =>"
                        + " tray: " + trayCount
                        + " sharedPrefs: " + sharedPrefsCount);
            }
            break;
            case INTENT_MODE_WRITE: {
                mCount++;
                LogUtil.debug(TAG, "Entering write in other process: counter = " + mCount);
                mTrayPreferences.put(KEY_MULTIPROCESS_COUNTER_SERVICE_WRITE, mCount);
                mSharedPreferences.putInt(KEY_MULTIPROCESS_COUNTER_SERVICE_WRITE, mCount);
            }
            break;
            default:
                throw new IllegalArgumentException("unknown mode");

        }
    }
}